/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-15
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "flash.h"
#include "app_skip_alg.h"
#include "app_global_data.h"
#include "app_store_set_info.h"
#include "bsp_hw_config.h"
#include "user_config.h"

/*----------------------------macro file---------------------------*/
#define STORE_SET_LOG_EN    0

#if STORE_SET_LOG_EN
#include "log.h"
#define STORE_SET_LOG       LOG_DEBUG
#else
#define STORE_SET_LOG(...)
#endif

#define STORE_INFO_VALID_VAL		0xFBEC

#define hal_store_erase_sector  hal_flash_erase_sector
#define hal_store_write         hal_flash_write
#define hal_store_read          hal_flash_read

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

static uint16_t app_store_checksum(uint8_t *pData, uint8_t len)
{
    uint16_t checksum = 0;

    for(uint8_t i=0;i<len;i++) {
        checksum += pData[i];
    }

    return checksum;
}

void app_set_info_revert(void)
{
    hal_store_erase_sector(SETTING_INFO_START_ADDR);
}

static void app_store_set_info_save(void)
{
    user_info_t user_info;

    user_info.valid_chk = STORE_INFO_VALID_VAL;
    user_info.set.timezone_mins = g_app_data.user.timezone_mins;
    user_info.set.countdown_time = g_app_data.user.countdown_time;
    user_info.set.countdown_num = g_app_data.user.countdown_num;
    user_info.checksum = app_store_checksum((uint8_t*)&user_info, sizeof(user_info) - 2);

    hal_store_erase_sector(SETTING_INFO_START_ADDR);
    hal_store_write(SETTING_INFO_START_ADDR, (uint8_t*)&user_info, sizeof(user_info_t));
}

void app_set_info_init(void)
{
    user_info_t user_info;

    hal_store_read(SETTING_INFO_START_ADDR, (uint8_t*)&user_info, sizeof(user_info));

    if(STORE_INFO_VALID_VAL == user_info.valid_chk && \
            user_info.checksum == app_store_checksum((uint8_t*)&user_info, sizeof(user_info) - 2))
    {
        if(user_info.set.timezone_mins > NEG_MINS_MAX_VAL \
            || (user_info.set.timezone_mins < NEG_MINS_MIN_VAL && user_info.set.timezone_mins > INT_MINS_MAX_VAL)) {
            g_app_data.user.timezone_mins = DEFAULT_TIMEZONE_MINS;
        }
        else {
            g_app_data.user.timezone_mins = user_info.set.timezone_mins;
        }

        if(user_info.set.countdown_time > DISP_SKIP_SEC_MAX_NUM || user_info.set.countdown_time <= 0) {
            g_app_data.user.countdown_time = DEFAULT_TIME_CNT_SECS;
        }
        else {
            g_app_data.user.countdown_time = user_info.set.countdown_time;
        }

        if(user_info.set.countdown_num > DISP_SKIP_CNT_MAX_NUM || user_info.set.countdown_num <= 0) {
            g_app_data.user.countdown_num = DEFAULT_SKIP_CNT_NUM;
        }
        else {
            g_app_data.user.countdown_num = user_info.set.countdown_num;
        }
    }
    else {
        g_app_data.user.timezone_mins = DEFAULT_TIMEZONE_MINS;
        g_app_data.user.countdown_time = DEFAULT_TIME_CNT_SECS;
        g_app_data.user.countdown_num = DEFAULT_SKIP_CNT_NUM;
    }
}


void app_set_info_local_timezone_save(uint16_t timezone_min)
{
    app_user_info_t *user_info = &g_app_data.user;

    if(user_info->timezone_mins == timezone_min || timezone_min > NEG_MINS_MAX_VAL \
        || (timezone_min < NEG_MINS_MIN_VAL && timezone_min > INT_MINS_MAX_VAL)) {
        return;
    }

    user_info->timezone_mins = timezone_min;
    app_store_set_info_save();
}


void app_set_info_countdown_time_save(uint16_t secs)
{
    app_user_info_t *user_info = &g_app_data.user;

    if(user_info->countdown_time == secs || secs > DISP_SKIP_SEC_MAX_NUM || secs <= 0)
        return;

    STORE_SET_LOG("set_cnt_down_secs: %d.\n" ,secs);
    user_info->countdown_time = secs;
    app_store_set_info_save();
}


void app_set_info_coutdown_num_save(uint16_t cnt)
{
    app_user_info_t *user_info = &g_app_data.user;

    if(user_info->countdown_num == cnt || cnt > DISP_SKIP_CNT_MAX_NUM || cnt <= 0)
        return;

    STORE_SET_LOG("set_cnt_jmp: %d.\n" ,cnt);
    user_info->countdown_num = cnt;
    app_store_set_info_save();
}

#if(DEV_MAC_BURN_ENABLE)
void app_set_info_burn_mac_save(uint8_t* mac, uint8_t len)
{
    if(DEV_MAC_LEN != len)
        return;

    burn_mac_t burn_mac;

    burn_mac.valid_chk = STORE_INFO_VALID_VAL;
    memcpy(burn_mac.mac, mac, len);
    burn_mac.checksum = app_store_checksum((uint8_t*)&burn_mac, sizeof(burn_mac) - 2);

    hal_store_erase_sector(BURN_INFO_START_ADDR);
    hal_store_write(BURN_INFO_START_ADDR, (uint8_t*)&burn_mac, sizeof(burn_mac_t));
}

uint8_t app_set_info_burn_mac_get(uint8_t* mac)
{
    burn_mac_t burn_mac;
    hal_store_read(BURN_INFO_START_ADDR, (uint8_t*)&burn_mac, sizeof(burn_mac_t));

    if(STORE_INFO_VALID_VAL == burn_mac.valid_chk && \
            burn_mac.checksum == app_store_checksum((uint8_t*)&burn_mac, sizeof(burn_mac_t) - 2))
    {
        memcpy(mac, burn_mac.mac, DEV_MAC_LEN);
        return DEV_MAC_LEN;
    }
    else {
        return 0;
    }
}

#endif


bool app_set_info_ble_adv_name_save(uint8_t* name, uint8_t len)
{
    if(len > APP_DEVICE_NAME_MAX_LEN)
        return false;

    ble_adv_name_t ble_adv_name;

    ble_adv_name.valid_chk = STORE_INFO_VALID_VAL;
    ble_adv_name.name_len = len;
    memcpy(ble_adv_name.name, name, len);
    ble_adv_name.checksum = app_store_checksum((uint8_t*)&ble_adv_name, sizeof(ble_adv_name) - 2);

    hal_store_erase_sector(BLE_ADV_NAME_START_ADDR);
    hal_store_write(BLE_ADV_NAME_START_ADDR, (uint8_t*)&ble_adv_name, sizeof(ble_adv_name_t));

    return true;
}


uint8_t app_set_info_ble_adv_name_get(uint8_t* name)
{
    ble_adv_name_t ble_adv_name;
    hal_store_read(BLE_ADV_NAME_START_ADDR, (uint8_t*)&ble_adv_name, sizeof(ble_adv_name_t));

    if(STORE_INFO_VALID_VAL == ble_adv_name.valid_chk && \
            ble_adv_name.checksum == app_store_checksum((uint8_t*)&ble_adv_name, sizeof(ble_adv_name_t) - 2))
    {
        memcpy(name, ble_adv_name.name, ble_adv_name.name_len);
        return ble_adv_name.name_len;
    }
    else {
        return 0;
    }
}
